//
//  CommentaryDataHandle.m
//  LOLProject
//
//  Created by lanou3g on 15/5/30.
//  Copyright (c) 2015年 lanou3g.com蓝欧科技. All rights reserved.
//

#import "CommentaryDataHandle.h"

@implementation CommentaryDataHandle

static CommentaryDataHandle * commentaryDataHandle=nil;

//创建解说页面的单例
+ (CommentaryDataHandle *) sharedInstance{
    @synchronized (self){
        if (commentaryDataHandle==nil) {
            commentaryDataHandle=[[CommentaryDataHandle alloc] init];
        }
        return commentaryDataHandle;
    }
}

//从网上请求数据
- (void) requestCommentaryData:(NSInteger) selectedSegmentIndex{
    if (selectedSegmentIndex==0) {
        [self requestCommentaryDataGameCommentary];
    }else if (selectedSegmentIndex==1){
        [self requestCommentaryDataHeroVideo];
    }else if (selectedSegmentIndex==2){
        [self requestCommentaryDataDMXY];
    }else{
        return ;
    }
    return ;
}

//从网上请求游戏解说数据
- (void) requestCommentaryDataGameCommentary{
//    异步连接下的GET请求
//    1.创建网址对象
    NSURL * url=[NSURL URLWithString:kGameCommentaryData_List_URL];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
//    用来存储封装好的数据
    NSMutableArray * array=[NSMutableArray array];
    __weak CommentaryDataHandle * commentaryDataHandle=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSArray * sourceArray=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
        for (NSDictionary * dic in sourceArray) {
            Commentary * commentary=[[Commentary alloc] init];
            [commentary setValuesForKeysWithDictionary:dic];
            if ([commentary.flmc isEqualToString:@"最新视频"]) {
                continue;
            }
            [array addObject:commentary];
        }
//        回调gameCommentaryBlock
        commentaryDataHandle.gameCommentaryBlock(array);
    }];
    return ;
}

//从网上请求英雄视频数据
- (void) requestCommentaryDataHeroVideo{
//    异步连接下的GET请求
//    1.创建网址对象
    NSURL * url=[NSURL URLWithString:kHeroVideoData_Collection_URL];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
//    用来存储封装好的数据
    NSMutableArray * array=[NSMutableArray array];
    __weak CommentaryDataHandle * commentaryDataHandle=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
        for (NSDictionary * dic in [sourceDic objectForKey:@"all"]) {
            Hero * hero=[[Hero alloc] init];
            [hero setValuesForKeysWithDictionary:dic];
            [array addObject:hero];
        }
//        回调heroVideoBlock
        commentaryDataHandle.heroVideoBlock(array);
    }];
    return ;
}

//从网上请求德玛西亚数据
- (void) requestCommentaryDataDMXY{
//    还需要进一步完善(先使用固定数据)
    [self createCommentaryDataDMXY];
    return ;
}

//创建德玛西亚数据
- (void) createCommentaryDataDMXY{
    DMXY * luoShiDai2=[[DMXY alloc] initWithImgName:@"luoshidai2.png" Name:@"撸时代 第二季" Kind:@"搞笑/青春/竞技" Published:@"2014-11-26"];
    DMXY * luoShiDai1=[[DMXY alloc] initWithImgName:@"luoshidai1.png" Name:@"撸时代 第一季" Kind:@"青春/搞笑" Published:@"2013-10-18"];
    DMXY * dmxy4=[[DMXY alloc] initWithImgName:@"dmxy4.png" Name:@"德玛西亚 第四季" Kind:@"搞笑/校园/战争" Published:@"2013-12-06"];
    DMXY * dmxy3=[[DMXY alloc] initWithImgName:@"dmxy3.png" Name:@"德玛西亚 第三季" Kind:@"搞笑/青春/竞技" Published:@"2013-02-14"];
    DMXY * dmxy2=[[DMXY alloc] initWithImgName:@"dmxy2.png" Name:@"德玛西亚 第二季" Kind:@"搞笑/青春/竞技" Published:@"2012-06-13"];
    DMXY * dmxy1=[[DMXY alloc] initWithImgName:@"dmxy1.png" Name:@"德玛西亚 第一季" Kind:@"搞笑/青春/竞技" Published:@"2011-12-09"];
    NSMutableArray * array=[NSMutableArray arrayWithObjects:luoShiDai2,luoShiDai1,dmxy4,dmxy3,dmxy2,dmxy1, nil];
//    回调videoGameBlock
    self.dMXYBlock(array);
    return ;
}

@end
